
import Cfg from './config.js';
import Msg from './message.js';
const EFFECT_IN = ['none','slide-in-right','slide-in-left','slide-in-top','slide-in-bottom','pop-in','fade-in','zoom-out','zoom-fade-out'];
const EFFECT_OUT = ['none','slide-out-right','slide-out-left','slide-out-top','slide-out-bottom','pop-out','fade-out','zoom-in','zoom-fade-in'];
//通用跳转
const navTo = (param) =>{		
	let url = param.url || '';
	let type = param.type || 0;
	let ani_type = param.ani || 'none';	
	if(Cfg.cfg.effect_on){
		let index = Math.floor(Math.random()*(EFFECT_IN.length+1));
		ani_type = EFFECT_IN[index]; 
	}		
	if(url==''){
		Msg.Fail('navTo函数传入参数有问题，请检查！');
		return false;		
	}
	if(url.substr(0,1)!='/')url='/'+url;
	//console.log(ani_type)
	switch(type){		
		case 0://不关本页到目的页
			uni.navigateTo({
				url:url,
				animationType:ani_type,
				fail:(e)=>{
					console.log(e)
				}
			});
			break;
		case 1://只关闭当前页到目的页
			uni.redirectTo({
				url:url,
				animationType:ani_type,
				fail:(e)=>{
					console.log(e)
				}
			});
			break;
		case 2://关闭所有页到目的页
			uni.switchTab({
				url:url,
				animationType:ani_type,
				fail:(e)=>{
					console.log(e)
				}
			})
		break;	
	}
}

const goPage = (e) => {
	let obj = e.currentTarget.dataset;			
	let url = obj.url || '';
	let type = obj.type || 0;
		type = parseInt(type)	
	if(url!=''&&url!=null){	
		if(url.indexOf('http')!=-1){
			// #ifdef H5
			location.href = url;
			return false;
			// #endif			
		}				
		navTo({url:url,type:type});
	}		
}


module.exports = {    
	_to:navTo,
	goPage
}